package com.cb.kfc.controller;


import com.cb.kfc.domain.Role;
import com.cb.kfc.domain.UserInfo;
import com.cb.kfc.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    //给用户添加角色
    @RequestMapping("/addRoleToUser.do")
    public String addRoleToUser(@RequestParam(name = "userId", required = true) String userId, @RequestParam(name = "ids", required = true) String[] roleIds) {
        userService.addRoleToUser(userId, roleIds);
        return "redirect:findAll.do";
    }

    //查询用户以及用户可以添加的角色
    @RequestMapping("/findUserByIdAndAllRole.do")
    public ModelAndView findUserByIdAndAllRole(@RequestParam(name = "id", required = true) String userid) throws Exception {
        ModelAndView mv = new ModelAndView();
        //1.根据用户id查询用户
        UserInfo userInfo = userService.findById(userid);
        //2.根据用户id查询可以添加的角色
        List<Role> otherRoles = userService.findOtherRoles(userid);
        mv.addObject("user", userInfo);
        mv.addObject("roleList", otherRoles);
        mv.setViewName("user-role-add");
        return mv;
    }
    //查询指定id的用户
    @RequestMapping("/findById.do")
    public ModelAndView findById(String id) throws Exception{
        ModelAndView mv = new ModelAndView();
        UserInfo userInfo = userService.findById(id);
        mv.addObject("user",userInfo);
        mv.setViewName("user-show1");
        return mv;
    }

    //用户添加
    @RequestMapping("/save.do")
    public String save(UserInfo userInfo) throws Exception {
        userService.save(userInfo);
        return "redirect:findAll.do";
    }

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page, @RequestParam(name = "size",required = true,defaultValue = "4")Integer size )throws Exception {
        ModelAndView mv = new ModelAndView();
        List<UserInfo> userList = userService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(userList);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("userList", userList);
        mv.setViewName("user-list");
        return mv;
    }

    /**
     * 删除
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete.do")
    public String delete(String[] ids) throws Exception{
        Integer  integer = userService.deleteUserById(ids);
        return "redirect:findAll.do";
    }

        /*@RequestMapping("/test.do")
        public String test(Model model)
        {
            model.addAttribute("name","I'm thymeleaf !");
            return "test";
        }*/
        @RequestMapping("/test.do")
        public ModelAndView test() {
            ModelAndView mv = new ModelAndView();

            mv.setViewName("main");
            mv.addObject("name","name");
            return mv;
        }
    @RequestMapping("/main.do")
    public ModelAndView main() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
         return mv;
    }

    @RequestMapping("/user-add.do")
    public ModelAndView userAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user-add");
        return mv;
    }
    @RequestMapping("/error.do")
    public ModelAndView errorPages() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("errorPages");
        return mv;
    }
    //用于跳转到，展示给客户看的前端页面
    @RequestMapping("/display.do")
    public ModelAndView display() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Displaypage");
        return mv;
    }
}
